// 服务员角色功能实现 
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include "RoleOfWaiter.h"

// 登录用户名
extern char loginUserName[10]; 	

// 输出了服务员功能界面  以及相关功能函数
// 返回一个整型数字，用来判断注销还是退出
int UiOfWaiter()
{
	int chooseItem;
	ListPointer tablePointer;
	FILE *filePointer;
	void(*waiterFunctionUi[5])(ListPointer tablePointer) = { WaiterOpenTable, 
		WaiterOrderDishes, WaiterCheckout, WaiterSearchTurnover, WaiterSearchOrders };
	filePointer = FileOpen("tableInfos.txt");
	tablePointer = FileRead(filePointer, sizeof(struct Table));
	while (1)
	{
		system("cls");
		printf("\n\n\t\t*************** 服务员功能界面 ***************\n\n\n");
		printf("\t\t\t\t 1: 订桌\n\n");
		printf("\t\t\t\t 2: 点菜\n\n");
		printf("\t\t\t\t 3: 结账\n\n");
		printf("\t\t\t\t 4: 营业额查询\n\n");
		printf("\t\t\t\t 5: 已购/未购单查询\n\n");
		printf("\t\t\t\t 6: 注销\n\n");
		printf("\t\t\t\t 7: 退出\n\n");
		printf("\t\t************************************************\n\n");
		printf("\t\t==> 请输入您要选择的功能：");
		chooseItem = FormatInputString(NULL, '1', '7', 1, 2);
		if (chooseItem > 0 && chooseItem < 6)
		{
			waiterFunctionUi[chooseItem - 1](tablePointer);
		}
		else if (chooseItem == 6)
		{
			return 0;
		}
		else if (chooseItem == 7)
		{
			return 1;
		}
	}
}
// 服务员订桌功能 
// tablePointer: 餐桌链表的头指针
void WaiterOpenTable(ListPointer tablePointer)
{
	int n;
	struct Table *table;
	char tableNumber[8] = { 0 };
	ListPointer listPointer;
	FILE *filePointer;
	filePointer = FileOpen("tableInfos.txt");
	while (1)
	{
		system("cls");
		n = TableSearch(tablePointer, 0, 3);// 打印空闲和预定的台桌
		if (n == 0)
		{
			printf("，暂无空闲桌子，按任意键返回...");
			getch();
			return;
		}
		printf("\n\t\t==> 请输入要订的餐桌号：");
		FormatInputString(tableNumber, '0', '9', 3, 2);
		listPointer = tablePointer->listNext;
		while (listPointer != NULL)
		{
			table = (struct Table *)(listPointer->listData);
			if (strcmp(table->tableNo, tableNumber) == 0)
			{
				if ((table->tableState == 0) || (table->tableState == 3))
				{
					break;
				}
			}
			listPointer = listPointer->listNext;
		}
		if (listPointer == NULL)
		{
			memset(tableNumber, 0, sizeof(char));
			printf("\n\t\t!!! 餐桌号有误或不可用,请重新输入，按任意键继续...");
			getch();
		}
		else
		{
			break;
		}
	}
	// 若为预定状态，直接跳转为点菜界面
	if (table->tableState == 3)
	{
		table->tableState = 2;   //占用，转到点菜界面
		FileWrite(filePointer, tablePointer, sizeof(struct Table), 0);   // 更新餐桌文件
		printf("\n\t\t!!! 订桌成功，按任意键继续...");
		getch();
		WaiterOrderDishes(tablePointer);   // 跳转到点菜界面
		return;
	}
	printf("\n\t\t==> 是否马上开始点菜(y)确定、(n)返回：");
	if (FormatInputString(NULL, 0, 0, 1, 1) != 'y')
	{
		table->tableState = 3;   //预定状态
		FileWrite(filePointer, tablePointer, sizeof(struct Table), 0);
		printf("\n\t\t!!! 预定成功，按任意键继续...");
		getch();
	}
	else
	{
		table->tableState = 2;   //占用，转到点菜界面
		FileWrite(filePointer, tablePointer, sizeof(struct Table), 0);
		printf("\n\t\t!!! 订桌成功，按任意键继续...");
		getch();
		WaiterOrderDishes(tablePointer);
		return;
	}
}
// 服务员选菜功能 
// tablePointer: 餐桌链表的头指针 
void WaiterOrderDishes(ListPointer tablePointer)
{
	int n;
	int count;
	char txt[] = ".txt";
	char tableNumber[8] = { 0 };
	char tablePath[30] = "";   // 详单路径
	struct DishOrder *newDishOrder;
	struct DishOrder *listData;
	FILE *filePointer;
	ListPointer menuPointer;
	ListPointer orderPointer;
	ListPointer detailOrderPointer;
	ListPointer listPointer;
	filePointer = FileOpen("dishMenus.txt");
	menuPointer = FileRead(filePointer, sizeof(struct Menu));
	filePointer = FileOpen("tempDishOrders.txt");
	orderPointer = FileRead(filePointer, sizeof(struct DishOrder));
	while (1)
	{
		system("cls");
		// 判断是否有可用桌子
		n = TableSearch(tablePointer, 2, 4);
		if (n == 0)
		{
			printf("，暂无可用桌子，按任意键继续...");
			getch();
			return;
		}
		printf("\n\t\t==> 请输入要点菜的餐桌号：");
		FormatInputString(tableNumber, '0', '9', 3, 2);
		strcat(tablePath, tableNumber);
		strcat(tablePath, txt);   // 获取详单路径
		listPointer = orderPointer->listNext;
		while (listPointer)
		{
			listData = (struct DishOrder *)(listPointer->listData);
			// 判断当前未购订单中是否存在此桌的订单
			if (strcmp(listData->orderTableNo, tableNumber) == 0)
			{
				newDishOrder = listData;
				filePointer = FileOpen(tablePath);
				detailOrderPointer = FileRead(filePointer, sizeof(struct DetailOrder));
				break;
			}
			listPointer = listPointer->listNext;
		}
		// 未购订单不存在输入桌的订单
		if (listPointer == NULL)
		{
			listPointer = tablePointer->listNext;
			while (listPointer)
			{
				if (strcmp(((struct Table *)(listPointer->listData))->tableNo, tableNumber) == 0)
				{
					if (((struct Table *)(listPointer->listData))->tableState == 2)
					{
						// 初始化一个订单节点
						newDishOrder = DishOrderInit(tableNumber);
						LinkedListAdd(orderPointer, newDishOrder);
						// 写入未购订单文件
						FileWrite(filePointer, orderPointer, sizeof(struct DishOrder), 0);
						// w+方式打开详单文件，开始点菜
						filePointer = fopen(tablePath, "w+");
						detailOrderPointer = FileRead(filePointer, sizeof(struct DetailOrder));
						break;
					}
					else if (((struct Table *)(listPointer->listData))->tableState == 0)
					{
						printf("\n\t\t==> 此桌号为空闲需先订桌才能点菜，是否订桌，(y)订桌、(n)	返回：");
							if (FormatInputString(NULL, 0, 0, 1, 1) == 'y')
							{
								WaiterOpenTable(tablePointer);// 跳转到订桌界面
							}
							return;
					}
				}
				listPointer = listPointer->listNext;
			}
			if (listPointer == NULL)
			{
				memset(tableNumber, 0, sizeof(char));
				strcpy(tablePath, "");
				printf("\n\t\t==> 输入的桌号有误或者处于维修状态，请重新输入，按任意键继续");
				getch();
			}
			else
			{
				break;
			}
		}
		else
		{
			break;
		}
	}
	DishOrderPrint(menuPointer, detailOrderPointer, newDishOrder);
	count = LinkedListCount(menuPointer);
	while (1)
	{
		CleanRow(count + 3, count + 12);
		SetCursorPosition(count + 3, 0);
		printf("\t\t[1]: 点菜\n");
		printf("\t\t[2]: 退菜\n");
		printf("\t\t[3]: 返回\n");
		printf("\t\t==> 请输入要操作的功能：");
		n = FormatInputString(NULL, '1', '3', 1, 2);
		switch (n)
		{
		case 1:
			{
				DishOrderAdd(count, newDishOrder, detailOrderPointer,
					orderPointer, menuPointer, tablePath);   // 加菜
				break;
			}
		case 2:
			{
				DishOrderDelete(count, newDishOrder, detailOrderPointer,
					menuPointer, orderPointer, tablePath);   // 减菜
				break;
			}
		case 3:
			break;
		}
		if (n == 3)
		{
			break;
		}
	}
	LinkedListFree(orderPointer);
	LinkedListFree(menuPointer);
	LinkedListFree(detailOrderPointer);
}
// 服务员查询营收额
// tablePointer: 餐桌链表的头指针 
void WaiterSearchTurnover(ListPointer tablePointer)
{
	int year;
	int month;
	int day;
	long *startDate;
	long *endDate;
	long orderNumber;   // 订单号
	long money = 0;   // 总营业额
	struct DishOrder *data;
	ListPointer orderPointer;
	ListPointer listPointer;
	FILE *filePointer;
	startDate = (long *)malloc(sizeof(long));   // 开始日期
	endDate = (long *)malloc(sizeof(long));   // 结束日期
	filePointer = FileOpen("dishOrders.txt");
	orderPointer = FileRead(filePointer, sizeof(struct DishOrder));
	system("cls");
	printf("\n\n\t\t*************** 营收额-查询条件选项 ***************\n\n");
	printf("\t\t\t\t    [1] 当天查询\n\n");
	printf("\t\t\t\t    [2] 历史查询");
	printf("\n\n\t\t==> 请输入要查询的条件:");
	if (FormatInputString(NULL, '1', '2', 1, 2) == 1)
	{
		system("cls");
		*startDate = OrderNoGenerate(NULL, NULL);   // 获取当前系统日期
		*endDate = *startDate;
	}
	else
	{
		OrderDateInputFormat(startDate, endDate);	// 输入开始和结束日期
		printf("\n");
	}
	printf("\t\t%-16s%-18s%-11s\n", "订单号", "时间", "金额");
	listPointer = orderPointer->listNext;
	while (listPointer)
	{
		data = (struct DishOrder *)(listPointer->listData);
		sscanf(data->orderNo, "%8ld", &orderNumber);   // 字符串转化成长整型
		if ((strcmp(data->orderStaff, loginUserName) == 0) && 
			((orderNumber <= *endDate) && (orderNumber >= *startDate)))
		{
			money = money + (data->orderTotalMoney);   // 营业额累加
			sscanf(data->orderNo, "%4d%2d%2d", &year, &month, &day);
			printf("\t\t%-16s%d%s%2d%s%2d%-6s%s%d\n", 
				data->orderNo, year, "年", month, "月", day, "日", "￥", data->orderTotalMoney);
		}
		listPointer = listPointer->listNext;
	}
	printf("\n\t\t营业额总额：￥%ld\n", money);
	LinkedListFree(orderPointer);
	printf("\n\t\t按任意键返回...");
	getch();
}
// 已购/未购订单查询功能
// tablePointer: 餐桌链表的头指针
void WaiterSearchOrders(ListPointer tablePointer)
{
	int chooseItem;
	FILE *filePointer;
	ListPointer orderPointer;
	filePointer = FileOpen("tempDishOrders.txt");
	orderPointer = FileRead(filePointer, sizeof(struct DishOrder));
	while (1)
	{
		system("cls");
		printf("\n\n\t\t*************** 已购/未购单查询界面 ***************\n\n");
		printf("\t\t\t\t  [1] 未购单查询\n\n");
		printf("\t\t\t\t  [2] 已购单查询\n\n");
		printf("\t\t\t\t  [3] 退出");
		printf("\n\n\t\t==> 请输入要查询的条件:");
		chooseItem = FormatInputString(NULL, '1', '3', 1, 2);
		switch (chooseItem)
		{
		case 1:
			// 未购订单查询
			DishOrderUnbuySearch(orderPointer);
			getch();
			break;
		case 2:
			// 已购订单查询
			DishOrderBuySearch();
			getch();
			break;
		case 3:
			break;
		}
		if (chooseItem == 3)
		{
			break;
		}
	}
	LinkedListFree(orderPointer);   // 释放链表
}
struct DishOrder * data;
FILE *filePointer;
// 结账功能 
// tablePointer: 餐桌链表的头指针
void WaiterCheckout(ListPointer tablePointer)
{
	int money;
	char txt[] = ".txt";
	char str[6] = { 0 };
	char str1[6] = { 0 };
	char tablePath[30] = "";   // 未结详单路径
	ListPointer listPointer;
	ListPointer detailOrderPointer;
	ListPointer orderPointer;
	ListPointer tempPointer;
	// 打开未购订单文件
	filePointer = FileOpen("tempDishOrders.txt");
	orderPointer = FileRead(filePointer, sizeof(struct DishOrder));
	while (1)
	{
		system("cls");
		listPointer = orderPointer->listNext;
		//判断当前是否有未购订单
		if (listPointer == NULL)
		{
			printf("\n\t\t!!! 目前没有未买订单，按任意键返回...");
			return;
		}
		printf("\t\t%12s%12s%11s%11s\n", "订单号", "餐桌号", "金额", "状态");
		while (listPointer)
		{
			data = (struct DishOrder *)(listPointer->listData);
			// 打印所有未结订单信息
			printf("\t\t%12s%12s%11d%11s\n",data->orderNo, data->orderTableNo, 
				data->orderTotalMoney, "未支付");
			listPointer = listPointer->listNext;
		}
		printf("\n\t\t==> 请输入要结账的餐桌号(3位)：");
		FormatInputString(str, '0', '9', 3, 2);
		tempPointer = orderPointer;
		listPointer = orderPointer->listNext;
		while (listPointer)
		{
			data = (struct DishOrder *)(listPointer->listData);
			// 找出要结账的订单节点
			if (strcmp(data->orderTableNo, str) == 0)
			{
				break;
			}
			tempPointer = listPointer;
			listPointer = listPointer->listNext;
		}
		// 未找到订单，提示
		if (listPointer == NULL)
		{
			printf("\n\n\t\t==> 餐桌号无订单或有误，按<Esc>键返回、其他键重新输入：");
			if (getch() == 27)
			{
				return;
			}
			memset(str, 0, sizeof(char));
		}
		else
		{
			break;
		}
	}
	// 获取详单路径
	strcat(tablePath, str);
	strcat(tablePath, txt);
	filePointer = FileOpen(tablePath);   // 打开将结账的详单文件
	// 读出未购详单链表
	detailOrderPointer = FileRead(filePointer, sizeof(struct DetailOrder));
	// 打印未购详单信息
	DetailOrderPrint(detailOrderPointer, data);
	printf("\n\t\t==> 订单号为%s是否结账，(y)确定、(n)返回：", data->orderNo);
	if (FormatInputString(NULL, 0, 0, 1, 1) == 'y')
	{
		printf("\n\t\t==> 实收金额：");
		money = FormatInputString(str1, '0', '9', 5, 2);   // 输入金额
		// 实收金额不可小于订单金额
		if (money < data->orderTotalMoney)
		{
			printf("\n\t\t==> 实收金额小于订单金额，无法结账，按任意键返回...");
			getch();
			return;
		}
		printf("\n\t\t==> 回找金额：%d", money - (data->orderTotalMoney));
		filePointer = FileOpen("detailOrders.txt");
		// 写入已结详单链表
		FileWrite(filePointer, detailOrderPointer, sizeof(struct DetailOrder), 2);
		filePointer = FileOpen("dishOrders.txt");
		fseek(filePointer, 0, 2);
		//写入结账服务员登录名到订单结构体中
		strcpy(data->orderStaff, loginUserName);
		//写入已结订单的结点到另一个文件
		fwrite(data, sizeof(struct DishOrder), 1, filePointer);
		fflush(filePointer);
		tempPointer->listNext = listPointer->listNext;   // 删除结点
		free(listPointer);   //释放已结订单的结点
		// 刷新未结订单的文件
		filePointer = fopen("tempDishOrders.txt", "w+");
		FileWrite(filePointer, orderPointer, sizeof(struct DishOrder), 0);
		listPointer = tablePointer->listNext;
		while (listPointer)
		{
			// 找到已结账桌节点
			if (strcmp(((struct Table *)(listPointer->listData))->tableNo, str) == 0)
			{
				// 桌子状态改为空闲
				((struct Table *)(listPointer->listData))->tableState = 0;
				filePointer = FileOpen("tableInfos.txt");
				// 更新餐桌信息文件
				FileWrite(filePointer, tablePointer, sizeof(struct Table), 0);
				break;
			}
			listPointer = listPointer->listNext;
		}
		printf("\n\t\t!!! 结账成功");
	}
	else
	{
		printf("\n\t\t!!! 结账失败");
	}
	printf("，按任意键返回...");
	getch();
	LinkedListFree(detailOrderPointer);
	LinkedListFree(orderPointer);
}
